<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/common.jspf"%>
<%@ include file="/common/taglibs.jspf"%>
<html lang="en">
	<head>
		<title><s:message code="login.ui.title" /></title>
		<script type="text/javascript"
	src="${contextPath }/theme/default/js/angular.js"></script>
<script type="text/javascript"
	src="${contextPath}/static/js/myUtils.js?v=20150911"></script>
</head>
	<body> 	
	<div> 
        <c:if test="${empty tenantConfig }">
          <div class="alert">
            <strong><s:message code="tenantConfig.error.empty"></s:message></strong>
          </div>
        </c:if>
		<div class="btn-toolbar">		  		
		  		<c:if test="${tenant.status==-2}">
		  		<button class="btn btn-warning" onclick="unlnockTenant();"><s:message code="tenant.func.unlock"/></button>
		  		</c:if>
		  		<c:if test="${tenant.status!=-2}">
		  		<button class="btn btn-warning" onclick="lockTenant();"><s:message code="tenant.func.lock"/></button>
		  		</c:if>
		  		<button class="btn btn-danger" onclick="deactivateTenant();"><s:message code="tenant.func.deactivate"/></button>
		  		
		</div>
		<ul class="nav nav-tabs" id="myTab">
		  <li class="active"><a href="#summary" data-toggle="tab"><s:message code="tenant.ui.tab.summary" /></a></li>
		  <li><a href="#stat" data-toggle="tab"><s:message code="tenant.ui.tab.stat" /></a></li>	
		  <li><a href="#conf" data-toggle="tab"><s:message code="tenant.ui.tab.conf" /></a></li>
		   <li><a href="#order" data-toggle="tab"><s:message code="tenant.ui.tab.tenantOrder" /></a></li>	    
		</ul>
		
		<div class="tab-content ">
		  <div class="tab-pane active" id="summary">		  	
		  	<form id="basic-form" action="index.do" method="GET" class="form-horizontal">
		  		<fieldset>		  			
		  			<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='tenant.tenantCode' /></label>
						<div class="controls">							
							<input type="text" name="tenantCode" value='<c:out value="${tenant.tenantCode }"/>' readonly="readonly" />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='tenant.tenantAlias' /></label>
						<div class="controls">							
							<input type="text" name="tenantAlias" value='<c:out value="${tenant.tenantAlias }"/>' readonly="readonly" />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='tenant.status' /></label>
						<div class="controls">							
							<input type="text" name="status" value='<s:message code="tenant.status.${tenant.status }"/>' readonly="readonly" />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='tenant.createBy' /></label>
						<div class="controls">							
							<input type="text" name="createUserId" value='<c:out value="${li:getUser(tenant.createBy).userName }"/>' readonly="readonly" />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='tenant.createDatetime' /></label>
						<div class="controls">							
							<input type="text" name="createDatetime" value='<c:out value="${li:formatDateTime(tenant.createDatetime) }"/>' readonly="readonly" />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='tenant.maxMembers' /></label>
						<div class="controls">				
							<input type="text" name="maxMembers" value='<c:out value="${tenant.maxMembers }"/>' readonly="readonly" />
                            &nbsp;<button type="button" class="btn btn-primary" onclick="updateSpace();" ><s:message code="tenant.func.upgrade" /></button>
						</div>
					</div>
                    <div class="control-group">
                      <label class="control-label visible-ie8 visible-ie9"><s:message code='tenant.maxStorage' /></label>
                      <div class="controls">        
                        <input type="text" name="maxStorage" value='<c:out value="${tenant.maxStorage }"/> (GB)' readonly="readonly" />
                        &nbsp;<button type="button" class="btn btn-primary" onclick="updateSpace();"><s:message code="tenant.func.upgrade" /></button>
                      </div>
                    </div>
					<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='tenant.trialDays' /></label>
						<div class="controls">							
							<input type="text" name="trialDays" value='<c:out value="${tenant.trialDays }"/>' readonly="readonly" />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='tenant.expiredDate' /></label>
						<div class="controls">							
							<input type="text" name="expiredDate" value='<c:out value="${li:formatDate(tenant.expiredDate) }"/>' readonly="readonly" />
							&nbsp;<button type="button" class="btn btn-primary" onclick="updateSpace();"><s:message code="tenant.func.recharge" /></button>
                            &nbsp;<button type="button" class="btn btn-primary" onclick="updateSpace();"><s:message code="tenant.func.upgrade" /></button>
						</div>
					</div>					
		  		</fieldset>		  		
		  	</form>
		  </div>	
		  <div class="tab-pane" id="stat">
		  	<form id="state-form" action="" method="POST" class="form-horizontal">
		  		<fieldset>
		  			<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='userState.totalLoginTimes' /></label>
						<div class="controls">							
							<input type="text" name="totalLoginTimes" value='<c:out value="${userState.totalLoginTimes }"/>' readonly="readonly" />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='userState.lastLoginDatetime' /></label>
						<div class="controls">							
							<input type="text" name="lastLoginDatetime" value='<c:out value="${userState.lastLoginDatetime }"/>' readonly="readonly" />
						</div>
					</div>
					<div class="control-group">
						<label class="control-label visible-ie8 visible-ie9"><s:message code='userState.lastLoginIp' /></label>
						<div class="controls">							
							<input type="text" name="lastLoginIp" value='<c:out value="${userState.lastLoginIp }"/>' readonly="readonly" />
						</div>
					</div>
				</fieldset>
			</form>		
		  </div>
		  <div class="tab-pane" id="conf">
			  <form id="conf-form" action="config.json" method="POST" class="form-horizontal">
		  		<fieldset>
		  		  <div class="control-group">
					<label class="control-label"><s:message code='tenant.tenantAlias' /></label>
					<div class="controls">							
						<input type="text" id="tenantAlias" name="tenantAlias" value='<c:out value="${tenant.tenantAlias }"/>'  />
					</div>
				  </div>
                </fieldset>
				<c:forEach items="${metaAttrGroups }" var="metaAttrGroup">
                  <fieldset>
                    <legend><s:message code='tenantConfig.${metaAttrGroup.code }' /></legend>
                    <c:forEach items="${metaAttrGroup.attrList }" var="metaAttr">
                      <c:set var="attrValue" value="${metaAttr.defaultValue }"></c:set>
                      <c:if test="${not empty APP_TENANT_CONFIG[tenant.id].confInfoMap[metaAttrGroup.code][metaAttr.key] }">
                        <c:set var="attrValue" value='${APP_TENANT_CONFIG[tenant.id].confInfoMap[metaAttrGroup.code][metaAttr.key] }'></c:set>
                      </c:if>    
                      <div class="control-group">
                        <label class="control-label"><s:message code='tenantConfig.${metaAttrGroup.code }.${metaAttr.key }' /></label>
                        <div class="controls">              
                          <input type="text" id="${metaAttrGroup.code }_${metaAttr.key }" name="${metaAttrGroup.code }_${metaAttr.key }" value='<c:out value="${attrValue }"/>'  />
                        </div>
                      </div>
                    </c:forEach>
                  </fieldset>
                </c:forEach>				
				<div class="form-actions">
					<button type="submit" class="btn btn-primary" data-loading-text="<s:message code='common.processing' />"><i class="icon-ok"></i> <s:message code="btn.submit" /></button>
				</div>
			</form>	
		  </div>	 
	      <div class="tab-pane" id="order" ng-controller="orderCtrl">
				<table class="table table-striped table-bordered table-hover datatable">
					<thead>
						<tr>
							<th><s:message code="tenantOrder.orderNo" /></th>
							<th><s:message code="tenantOrder.orderType" /></th>
							<th><s:message code="tenantOrder.bagCode" /></th>
							<th><s:message code="tenantOrder.price" /></th>
							<th><s:message code="tenantOrder.quantity" /></th>
							<th><s:message code="tenantOrder.status" /></th>
							<th><s:message code="common.field.createBy" /></th>
							<th><s:message code="common.field.createDatetime" /></th>
							<th><s:message code="common.action" /></th>
						</tr>
					</thead>
					<tbody>
							<tr ng-repeat="order in orders">

								<td>{{order.TenantOrder.orderNo }}</td>
								<td>{{order.TenantOrder.orderTypeName}}</td>
								<td>{{order.TenantOrder.productNo}}</td>
								<td>{{order.TenantOrder.price}}</td>
								<td>{{order.TenantOrder.quantity}}</td>
								<td>{{order.TenantOrder.orderStatusName}}</td>
								<td>{{order.UserInfo.userName}}</td>
								<td>{{order.TenantOrder.createDatetime|date:'yyyy-MM-dd HH:mm:ss' }}</td>
								<td>
									<div class="action-buttons">
									<a ng-click="goToPay(order.TenantOrder.id)" ng-if="order.TenantOrder.status <1" title="<s:message code='tenantOrder.func.goToPay'/>"><i class="blue icon-hand-right"></i></a>
									<a href="${contextPath}/system/tenantOrder/show.do?id={{order.TenantOrder.id}}" class="blue"  title="<s:message code='tenantOrder.func.watch'/>"><i class="icon-eye-open"></i></a>
										<a href="${contextPath}/system/tenantOrder/edit.do?id={{order.TenantOrder.id}}" class="blue" ng-if="order.TenantOrder.status <1" title="<s:message code='tenantOrder.func.edit'/>"><i class="icon-pencil"></i></a>&nbsp; 
										
										<a href="javascript:;" class="red" ng-click="rowDelete(order.TenantOrder.id)" ng-if="order.TenantOrder.status!=1"  title="<s:message code='tenantOrder.func.show'/>"><i class="icon-trash"></i></a>
									</div>
								</td>
							</tr>
					</tbody>
				</table>
			</div>
	  </div>
	</div>	 

<!-- inline scripts related to this page -->
<script src="${contextPath }/static/widgets/layer/layer.js"></script>

<script type="text/javascript">

$(document).ready(function(){	
	
	//$("#conf-form").validate({});	
	initAngular(document);
	showOrder("${tab}");
	tabOnShow("summary");
	tabOnShow("stat");
	tabOnShow("conf");
	tabOnShow("order");
	$('#conf-form').littFormSubmit({		
		rules : {
			tenantAlias: {
                required: true,
                minlength: 4,
                maxlength: 50
            }
		},	
		success: function(reply){			
			$.webtools.alert({
			  containerId: "conf-form"
			  ,type: "success",
				overwrite: false,
				position: "prepend",
				message: "<s:message code='tenant.func.conf.success' />"				
			}); 				
		}
	});
	
});	

	
function activateTenant()
{
	location.href = "${contextPath }/login/activateTenant.do";
}

function upgradeTenant()
{
	location.href = "${contextPath }/login/upgradeTenantPermission.do";		
}

function lockTenant()
{
	bootbox.confirm("<s:message code='tenant.func.lock.confirm' />", function(result){
		if(result)
		{
			$.webtools.ajax({
				url: "${contextPath }/login/lockTenant.json",
				params: {},
				success: function(reply) {
					location.reload();
				}
			});	
		}
	});	
}

function unlnockTenant()
{	
	bootbox.confirm("<s:message code='tenant.func.unlock.confirm' />", function(result){
		if(result)
		{
			$.webtools.ajax({
				url: "${contextPath }/login/unlockTenant.json",
				params: {},
				success: function(reply) {
					location.reload();
				}
			});	
		}
	});
	}

function deactivateTenant()
{
	bootbox.confirm("<s:message code='tenant.func.deactivate.confirm' />", function(result){
		if(result)
		{
			$.webtools.ajax({
				url: "${contextPath }/login/deactivateTenant.json",
				params: {},
				success: function(reply) {
					location.href="${contextPath}/main.do";
				}
			});	
		}
	});	
}

function showOrder(tab){
	var selector='#myTab a[href=#'+tab+']';
	$(selector).tab('show');
 
}

function tabOnShow(tab){
	var selector='#myTab a[href=#'+tab+']';
	  $(selector).on('show', function (e){
		  location.href="${contextPath}/system/tenant/index.do?tab="+tab;
		  e.target // activated tab
		  e.relatedTarget 
		 
		  
	  });

}

function updateSpace(){
	//location.href='${contextPath}/system/tenantOrder/spaceChoose.do';
	
	
	 layer.open({
		    type: 2,
		    title: false,
		    //closeBtn: true,
		    area: ['900px', '500px'],
		    skin: 'layui-layer-rim', //加上边框
		    shadeClose: true,
		    fix: false, //不固定
		    //maxmin: true,
		    content: '${contextPath}/system/tenantOrder/spaceChoose.do',
		    end: function(index){
		    	location.href="${contextPath}/system/tenant/index.do?tab=order";
		    }		
		  }); 

	
}






function orderCtrl($scope, $http) {
	$scope.orders=[];
	
	//初始化数据 var
	p = $http({
		method : 'POST',
		url : "${contextPath}/system/tenantOrder/getTenantOrderByTenant.json",
		params : {

		}
	});

	p.success(function(data) {
		$scope.orders= data.data;
	});
	
	
	$scope.rowDelete=function(id)
	{			
		bootbox.confirm("<s:message code='common.delete.confirm' />", function(result){
			if(result)
			{
				p = $http({
					url: "${contextPath}/system/tenantOrder/delete.json",
					method:"POST",
					params: {"id":id},
				});	
				
				p.success(function(data) {
					var ids=[];
					for (var i=0;i<$scope.orders.length;i++){
						ids.push($scope.orders[i].id);
					}
					$scope.orders.shift($.inArray(id,ids));
				}
				)};
			
		});
	}
	
	$scope.goToPay=function(id){
		 layer.open({
			    type: 2,
			    title: false,
			    //closeBtn: true,
			    area: ['600px', '400px'],
			    skin: 'layui-layer-rim', //加上边框
			    shadeClose: true,
			    fix: false, //不固定
			    //maxmin: true,
			    content: '${contextPath}/system/tenantOrder/pay.do?id='+id,
			    end: function(index){
			    	location.href="${contextPath}/system/tenant/index.do?tab=order";
			    }		
			  }); 
	}
	
}
	



</script>
</body>
</html>
